package com.timoon.testng.demo;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class UserJFrame extends JFrame implements ActionListener,ItemListener{
	private int number=1;
	private JTextField text_number,text_name;
	private JRadioButton radiobutton_male,radiobutton_female;
	private JComboBox combobox_province,combobox_city;
	private JButton button_add;
	private JTextArea text_user;
	
	
	public UserJFrame(){
		this.setSize(360,200);
		this.setLocation(300,240);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLayout(new GridLayout(1,2));
		text_user=new JTextArea();
		this.add(text_user);
		JPanel panel=new JPanel(new GridLayout(6,1));
		this.add(panel);
		
		text_number=new JTextField("1");
		text_number.setEditable(false);
		panel.add(text_number);
		text_name=new JTextField("姓名");
		panel.add(text_name);
		
		JPanel panel_radiobutton=new JPanel(new GridLayout(1,2));
		panel.add(panel_radiobutton);
		ButtonGroup buttongroup=new ButtonGroup();
		radiobutton_male=new JRadioButton("男",true);
		buttongroup.add(radiobutton_male);
		panel_radiobutton.add(radiobutton_male);
		
		radiobutton_female=new JRadioButton("女",true);
		buttongroup.add(radiobutton_female);
		panel_radiobutton.add(radiobutton_female);
		
		Object province[]={"江苏省","浙江省"};
		combobox_province=new JComboBox(province);
		combobox_province.addItemListener(this);
		panel.add(combobox_province);
				Object city[]={"南京市","苏州市","无锡市"};
		combobox_city=new JComboBox(city);
		panel.add(combobox_city);
		
		button_add=new JButton("添加");
		button_add.addActionListener(this);
		panel.add(button_add);
		this.setVisible(true);
	}
	
	public void itemStateChanged(ItemEvent e){
		if(combobox_province.getSelectedIndex()==0)
		{
		    combobox_city.removeAllItems();
		    combobox_city.addItem("南京市");
		    combobox_city.addItem("苏州市");
		    combobox_city.addItem("无锡市");
		}
    	if(combobox_province.getSelectedIndex()==1)
    	{
		    combobox_city.removeAllItems();
		    combobox_city.addItem("杭州市");
		    combobox_city.addItem("宁波市");
		    combobox_city.addItem("温州市");
    	}
	}
    
    public void actionPerformed(ActionEvent e){
    	if(e.getSource()==button_add){
       		String aline="";
    		aline=number+","+text_name.getText();
    		if(radiobutton_male.isSelected())
    		    aline+=","+radiobutton_male.getText();
    		if(radiobutton_female.isSelected())
    		    aline+=","+radiobutton_female.getText();
    		aline+=","+combobox_province.getSelectedItem();
    		aline+=","+combobox_city.getSelectedItem();
    		text_user.append(aline+"\n");
    		
    		this.number++;
    		text_number.setText(""+this.number);
    		text_name.setText("姓名");
    	}
    }
    public static void main(String arg[]){
    	new UserJFrame();
    	}
}